package com.sx.form.action;

import java.io.File;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ModelDriven;
import com.sx.article.biz.IArticleBiz;
import com.sx.article.biz.IAttachmentBiz;
import com.sx.article.entity.ArticleEntity;
import com.sx.article.entity.AttachmentEntity;
import com.sx.cms.entity.UserInfoEntity;
import com.sx.core.BaseAction;
import com.sx.form.ts.biz.IHouseInfoBiz;
import com.sx.form.ts.entity.HouseInfoEntity;

@SuppressWarnings("serial")
public class TsIndexAction extends BaseAction implements ModelDriven<ArticleEntity>{
	private Log log = LogFactory.getLog(this.getClass());
	private IArticleBiz articlebiz;
	private IAttachmentBiz attachmentbiz;
	private IHouseInfoBiz houseinfobiz;
	private ArticleEntity article = new ArticleEntity();
	private String filepath;
	/**
	 *跳转到添加房源信息页面 
	 **/
	public String goToEditInfoPage(){
		String page = ERROR;
		try {
			HouseInfoEntity houseinfo = new HouseInfoEntity();
			String type = getRequest().getParameter("type");
			UserInfoEntity userinfo = getUserInfoFromSession();
			if (type!=null&&userinfo!=null) {
				Integer id = null;
				houseinfo.setTitle("");
				houseinfo.setStauts(new Integer(0));
				houseinfo.setType(Integer.parseInt(type));
				houseinfo.setAuthorid(userinfo.getUserid());
				Integer count = houseinfobiz.findHouseInfoCount(houseinfo);
				if (count>0) {
					page = "infolist";
				} else {
					houseinfo.setTitle("[暂存]房源信息");
					houseinfo.setArea(new Double(100));
					id = this.houseinfobiz.addHouseInfoDraft(houseinfo);
					houseinfo.setId(id);
					page = "editinfo";
				}
				getRequest().setAttribute("houseinfo", houseinfo);
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
			log.error("跳转到添加房源信息页面",e);
		}
		return page;
	}
	
	/**
	 * 上传文件信息
	 **/
	public void uploadFile(){
		try {
			//1.按照文章编号去数据库中检查是否存在编号数量,如果已经5个不上传直接提示
			//2.如果不存在5开始上传
			//2.1上传成功后返回附件编号
			//2.2不成功返回success:false,前端接收到后不加载信息
			String filename = article.getFiledataFileName();
			File file = article.getFiledata();
			AttachmentEntity attachment = super.getUpdateFileInfo(getFilepath(), filename, file);
			outJsonString("{\"success\":\"true\",\"filepath\":\""+attachment.getFvaluepath()+"/"+attachment.getFvaluename()+"\"}");
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}
	/**
	 * 根据编号信息
	 **/
	public void findMsgInfoById(){
		
	}
	/**
	 * 首页列表数据信息
	 **/
	public void findNewsInfoForIndexPage(){
		try {
			
			List<ArticleEntity> list = articlebiz.findArticleInfos(article);
			String str = JSONArray.toJSONString(list);
			outJsonString(str);
		} catch (Exception e) {
			log.error("获取文章数据信息",e);
			// TODO: handle exception
		}
	}
	/**
	 * 跳转到注册界面信息
	 **/
	public String goToTSRegister(){
		return "tsregister";
	}
	/**
	 * 获取数据TableInfo
	 **/
	public void getInfoList(){
		Integer page = Integer.parseInt(getRequest().getParameter("page"));
		article.setStart((page-1)*article.getLimit());
		String str =  "{totalCount:0,data:[]}";
		try {
			str = articlebiz.findArticleInfoToArrayType(article,"pageCount","data");
		} catch (Exception e) {
			// TODO: handle exception
			log.error("新增文章数据信息",e);
		}
		outJsonString(str);
	}
	/**
	 *获取数据信息 
	 **/
	public void getInfomationByAid(){	
		try {
			ArticleEntity articleinfo = articlebiz.findArticleById(article);
//			articleinfo.setContent(MyUtils.convertContextInfo(articleinfo.getContent()));
			/*String _s = "<p>" +
			"<img alt='' src='/sx/front/attachment/articleimage/2012-10-11date/36680a45-856b-46c4-9f73-212f6ae4ef20.jpg' style='width: 204px; height: 195px' /></p>" +
			"<p>WWWWWSSSSSSSSSSSSSSS阿萨德发</p>" +
			"<p>送到分阿萨德发送到阿萨德法撒旦法撒旦</p>" +
			"<p>阿萨德法撒旦法撒旦法阿萨德法撒旦法撒旦法</p>" +
			"<p>阿萨德发送到发送到发送到</p>" +
			"<p>阿萨德发送到发送到发送到</p>" +
			"<p>萨到发送到发送到发送到飞洒到发送到分</p>";
			System.out.println("==>"+_s);
			System.out.println("==>"+_t);
			System.out.println("==>"+_t.replaceAll("\n\t", ""));
			articleinfo.setContent("<p>" +
					"<img alt='' src='/sx/front/attachment/articleimage/2012-10-11date/36680a45-856b-46c4-9f73-212f6ae4ef20.jpg' style='width: 204px; height: 195px' /></p>" +
					"<p>WWWWWSSSSSSSSSSSSSSS阿萨德发</p>" +
					"<p>送到分阿萨德发送到阿萨德法撒旦法撒旦</p>" +
					"<p>阿萨德法撒旦法撒旦法阿萨德法撒旦法撒旦法</p>" +
					"<p>阿萨德发送到发送到发送到</p>" +
					"<p>阿萨德发送到发送到发送到</p>" +
					"<p>萨到发送到发送到发送到飞洒到发送到分</p>");*/
			String _t = articleinfo.getContent();
			articleinfo.setContent(_t.replaceAll("\n\t", ""));
			String str = JSONObject.toJSONString(articleinfo);
			outJsonString(str);
		} catch (Exception e) {
			outJsonString("{}");
			// TODO: handle exception
			log.error("获取数据信息",e);
		}
	}
	/**
	 * @return the articlebiz
	 */
	public IArticleBiz getArticlebiz() {
		return articlebiz;
	}
	/**
	 * @param articlebiz the articlebiz to set
	 */
	public void setArticlebiz(IArticleBiz articlebiz) {
		this.articlebiz = articlebiz;
	}
	/**
	 * @return the attachmentbiz
	 */
	public IAttachmentBiz getAttachmentbiz() {
		return attachmentbiz;
	}
	/**
	 * @param attachmentbiz the attachmentbiz to set
	 */
	public void setAttachmentbiz(IAttachmentBiz attachmentbiz) {
		this.attachmentbiz = attachmentbiz;
	}
	public ArticleEntity getModel() {
		// TODO Auto-generated method stub
		return article;
	}
	/**
	 * @return the filepath
	 */
	public String getFilepath() {
		return filepath;
	}
	/**
	 * @param filepath the filepath to set
	 */
	public void setFilepath(String filepath) {
		this.filepath = filepath;
	}
	/**
	 * @return the houseinfobiz
	 */
	public IHouseInfoBiz getHouseinfobiz() {
		return houseinfobiz;
	}
	/**
	 * @param houseinfobiz the houseinfobiz to set
	 */
	public void setHouseinfobiz(IHouseInfoBiz houseinfobiz) {
		this.houseinfobiz = houseinfobiz;
	}
	
	
}
